package com.hxzy.controller.mobile.addressbook;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketingThreadLocalUtil;
import com.hxzy.controller.mobile.addressbook.req.AddressBookAddReq;
import com.hxzy.controller.mobile.addressbook.req.AddressBookEditReq;
import com.hxzy.controller.mobile.addressbook.resp.AddressBookResp;
import com.hxzy.controller.mobile.mapstruct.AddressBookConvert;
import com.hxzy.entity.AddressBook;
import com.hxzy.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author wy
 * @Description TODO
 * @Date 2023/7/24 9:14
 * @Version 1.0
 */
@Api(tags = "手机端-收货地址管理")
@RestController
@RequestMapping(value = "/mobile/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation(value = "获取用户默认收货地址")
    @GetMapping(value = "/default")
    public R<AddressBookResp> defaultAddress(){
        Integer userId = MarketingThreadLocalUtil.mobileThreadLocal.get().getId();

        LambdaQueryWrapper<AddressBook> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        //sys_normal_disable
        queryWrapper.eq(AddressBook::getIsDeleted,"0");
        //sys_is_frame
        queryWrapper.eq(AddressBook::getIsDefault, "0");
        AddressBook db = this.addressBookService.getOne(queryWrapper);
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(AddressBookConvert.INSTANCE.convert(db));
    }

    @ApiOperation("获取当前用户所有的收货地址")
    @GetMapping(value = "/data")
    public R<List<AddressBookResp>> search(){
        Integer userId = MarketingThreadLocalUtil.mobileThreadLocal.get().getId();

        LambdaQueryWrapper<AddressBook>  queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        //sys_normal_disable
        queryWrapper.eq(AddressBook::getIsDeleted,"0");
        //排序
        queryWrapper.orderByAsc(AddressBook::getIsDefault,AddressBook::getCreateTime);

        List<AddressBook> list = this.addressBookService.list(queryWrapper);
        //转换
        List<AddressBookResp> respList=AddressBookConvert.INSTANCE.convert(list);
        return R.okHasData(respList);
    }

    @ApiOperation(value = "新增收货地址")
    @PostMapping
    public R add(@RequestBody @Valid AddressBookAddReq req ){
        boolean result=this.addressBookService.add(req);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }

    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    public R<AddressBookResp> findById(@PathVariable(value = "id") Long id ){
        AddressBook db = this.addressBookService.getById(id);
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return  R.okHasData(AddressBookConvert.INSTANCE.convert(db));
    }

    @ApiOperation(value = "修改收货地址")
    @PutMapping
    public R update(@RequestBody @Valid AddressBookEditReq req ){
        AddressBook db = this.addressBookService.getById(req.getId());
        if(Objects.isNull(db)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }

        AddressBook entity = AddressBookConvert.INSTANCE.convert(req);
        entity.setIsDefault(db.getIsDefault());
        entity.setIsDeleted(db.getIsDeleted());

        boolean result=this.addressBookService.updateById(entity);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }


    @ApiOperation(value = "设定默认地址")
    @GetMapping(value = "/default/{id}")
    public R  updateDefaultAddress( @PathVariable(value = "id") Long id){
         boolean result=this.addressBookService.updateDefaultAddress(id);
        return result?R.ok(): R.build(AckCode.DATABASE_ERROR);
    }
}
